<?php

/**
 * ExtClientActionBase class file
 *
 * @author TrueSoftware B.V. (The Netherlands)
 * @link http://www.ext4yii.com
 * @copyright Copyright 2007-2010 TrueSoftwareB.V. all rights reserved.
 * @license http://www.ext4yii.com/license/
 *          EVALUATION VERSION - NOT FOR COMMERCIAL USE OR DISTRIBUTION.
 * @version 0.7.2
 */
abstract class ExtClientActionBase extends ExtActionBase
{

    protected $_isfunction;

    public function parseNode(ExtHTMLParserNode $node, ExtASTWalker $astwalker)
    {
        parent::parseNode($node, $astwalker);
        $handler = $this->parseScriptProperty('handler', null, $node, $astwalker);
        if (!is_null($handler))
            $this->setHandler($handler);
    }

    protected function checkSetHandler()
    {
        // if we have a handler
        if (trim($this->getHandler()) != '')
        {
            $this->_isfunction = false;
            return;
        }
        else if (trim($this->getFunction()) != '')
        {
            $this->_isfunction = true;
            $this->setHandler($this->getFunction());
            return;
        }
    }

    public function createContainer(ExtHTMLParserNode $node)
    {
        $node->shiftUp();
        return null;
    }

    public function renderConfig($asobject=true)
    {
        $this->checkSetHandler();
        $this->createEvent();
        return null;
    }

    public function createEvent()
    {
        $result = "";
        $args = $this->parent->getFunctionArgsByEventName($this->getOnEvent());
        $funcname = $this->parent->getId() . '_ext4yii_client_' . $this->getOnEvent();

        if ($this->_isfunction)
            $result = sprintf('function(%s){%s(%s)}', $args, $this->getHandler(), $args);
        else
        {
            $result = $this->wrapLooseScript($this->getHandler(), $args);
        }

        if (trim($this->getDelay()) != '')
            if ($this->_isfunction)
                $result = sprintf('function(%s){ %s.defer(%s,this,[%s]); return true; }',
                                $args,
                                $this->getHandler(),
                                $this->getDelay(),
                                $args);
            else
                $result = sprintf('function(%s){ var dly=%s; dly.defer(%s,this,[%s]); return true; }',
                                $args,
                                $result,
                                $this->getDelay(),
                                $args);

        $this->parent->setJavaScriptConfig($funcname, $result);
        $this->parent->addEvent($this->getOnEvent(), 'client', $args, $funcname);
    }

}

?>
